package com.ruoyi.web.controller.api;

import com.ruoyi.common.config.TxSdkConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.sdk.SignatureUtils;
import com.ruoyi.web.controller.common.CommonController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Random;

/**
 * @author zhimin.hu
 * @Date 2021/3/3
 */
@Api(value = "SDK相关接口",tags = "SDK相关接口")
@RestController
@RequestMapping("/api/sdk")
public class SDKApiController extends BaseApiController {
    private static final Logger logger = LoggerFactory.getLogger(CommonController.class);

    @Autowired
    private TxSdkConfig txSdkConfig;
   private SignatureUtils signatureUtils=new SignatureUtils();
    /**
     * 获取上传签名
     */
    @ApiOperation("获取腾讯云点播上传签名")
    @PostMapping("/getSignature")
    @ResponseBody
    public AjaxResult getSignature()
    {
        signatureUtils.setSecretId(txSdkConfig.getSecretId());
        signatureUtils.setSecretKey(txSdkConfig.getSecretKey());
        signatureUtils.setCurrentTime(System.currentTimeMillis() / 1000);
        signatureUtils.setRandom(new Random().nextInt(java.lang.Integer.MAX_VALUE));
        // 签名有效期：2天
        signatureUtils.setSignValidDuration(3600 * 24 * 2);
        try {
            String signature = signatureUtils.getUploadSignature();
            System.out.println("signature : " + signature);
            HashMap<String,String> result=new HashMap<>();
            result.put("signature",signature);
            return AjaxResult.success(result);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.toString());
            return AjaxResult.error("获取签名失败:"+e.getMessage());
        }

    }
}
